$(function () {
    $("#jqGrid").Grid({
        url: '../agent/vendorList',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '供应商商家ID', name: 'vendorRootDeptId', index: 'vendor_root_dept_id', hidden: true},
			{label: '供应商名称', name: 'vendorRootDeptName', index: 'vendorRootDeptName', width: 80},
			{label: '供应商手机', name: 'vendorRootDeptMobile', index: 'vendorRootDeptMobile', width: 80},
            {label: '供应商微信', name: 'vendorRootDeptWechat', index: 'vendorRootDeptWechat', width: 80},
			{label: '代理申请时间', name: 'applyTime', index: 'apply_time', width: 80, formatter: function (value) { return transDate(value)}},
			{label: '代理认证时间', name: 'certTime', index: 'cert_time', width: 80, formatter: function (value) { return transDate(value)}},
			{label: '认证等级', name: 'certLevel', index: 'cert_level', width: 80, align: 'center', formatter: function (value, options, row) {
                    if (value) {
                        if (value == 0) {
                            return '初级代理商'
                        }
                        if (value == 1) {
                            return '中级代理商'
                        }
                        if (value == 2) {
                            return '高级代理商'
                        }
                    }else{
                    	return '-'
					}
                }
			}
		]
    });

    $("#goodsJqGrid").Grid({
        url: '../goods/agentList',
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '商品类型', name: 'categoryName', index: 'category_id', width: 100},
            {label: '商品名称', name: 'name', index: 'name', width: 160},
            {label: '商品编码', name: 'goodsSn', index: 'goods_sn', width: 80},
            {
                label: '上架', name: 'isOnSale', index: 'is_on_sale', width: 50, align: 'center',
                formatter: function (value) {
                    return transIsNot(value);
                }
            },
            {
                label: '热销', name: 'isHot', index: 'is_hot', width: 50, align: 'center', formatter: function (value) {
                    return transIsNot(value);
                }
            },
            {label: '销售类型', name: 'saleType', index: 'sale_type', width: 80, align: 'center', formatter: function (value) {
                    switch(value){
                        case 'x':
                            return '现货';
                            break;

                        case 'q':
                            return '订货';
                            break;

                        case 'p':
                            return '拼单';
                            break;
                    }
                }
            },
            {label: '价格', name: 'agentPrice', index: 'agent_price', width: 80},
            /* {label: '普通客户价格', name: 'clientPrice', index: 'client_price', width: 80},
            {label: '高级客户价格', name: 'vipClientPrice', index: 'vip_client_price', width: 80},
            {label: '最低访问等级', name: 'accessUserGrade',index: 'access_user_grade', width: 80, align: 'center', formatter: function (value) {
                    switch(value){
                        case 0:
                            return '游客';
                            break;

                        case 1:
                            return '普通客户';
                            break;

                        case 2:
                            return '高级客户';
                            break;
                    }
                }
            },*/
            {label: '总销售量', name: 'sellVolume', index: 'sell_volume', width: 80}
        ]
    });
    $("#goodsJqGrid").setGridWidth($(window).width()*0.96);
});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		agent: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: ''
		},
        goodsKeyword: {
            name: ''
        }
	},
	methods: {
		query: function () {
			vm.reload();
		},
        queryByGoodsKeyword: function () {
            var page = $("#goodsJqGrid").jqGrid('getGridParam', 'page');

            $("#goodsJqGrid").jqGrid('setGridParam', {
                postData: {'key': vm.goodsKeyword.name, 'rootDeptId' : vm.agent.vendorRootDeptId},
                page: page
            }).trigger("reloadGrid");
        },
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.agent = {};
		},
		update: function (event) {
            let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            let url = vm.agent.id == null ? "../agent/save" : "../agent/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.agent),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../agent/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
        importAgentGoods: function (event) {
            let row = getSelectedRowData("#jqGrid");
            if (row == null || row.length == 0) {
                return;
            }

            vm.showList = false;
            vm.agent.vendorRootDeptId = row.vendorRootDeptId;
            vm.agent.vendorRootDeptName = row.vendorRootDeptName;
            vm.title = "导入供应商商品";

            vm.queryByGoodsKeyword();
        },
        addGoodsToMyShop: function(){
            let ids = getSelectedRows("#goodsJqGrid");
            if (ids == null){
                return;
            }

            //获取勾选商品
            let agentGoodsIds = "";
            for (var i=0; i<ids.length; i++) {
                agentGoodsIds += ids[i] + "_";
            }
            vm.agent.agentGoodsIds = agentGoodsIds;

            confirm('确定要导入选中的商品到我的店铺吗？', function () {
                Ajax.request({
                    url: "../agentgoods/addGoodsToMyShop",
                    params: JSON.stringify(vm.agent),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功！请点击“商品建档”菜单查看导入商品信息。', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
		getInfo: function(id){
            Ajax.request({
                url: "../agent/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.agent = r.agent;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                name: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});